from typing import Optional
from app.models.location import City, Province

class Settings:
    def __init__(self):
        self.PROVINCE: str = "湖北"
        self.CITY: str = "宜昌"
        self.LATITUDE: Optional[float] = 30.426120
        self.LONGITUDE: Optional[float] = 111.768550

    async def update_location(self):
        """根据当前省市查询并更新经纬度"""
        province = await Province.get_or_none(name_cn=self.PROVINCE)
        if not province:
            raise ValueError(f"省份 {self.PROVINCE} 不存在")

        city = await City.get_or_none(name_cn=self.CITY, province=province)
        if not city:
            raise ValueError(f"城市 {self.CITY} 不存在于省 {self.PROVINCE} 中")

        self.LATITUDE = float(city.latitude) if city.latitude else None
        self.LONGITUDE = float(city.longitude) if city.longitude else None

        print(f"当前地区已更新为：{self.PROVINCE}-{self.CITY} ({self.LATITUDE}, {self.LONGITUDE})")

    async def set_location(self, province: str, city: str):
        """修改省份和城市并更新经纬度"""
        self.PROVINCE = province
        self.CITY = city
        await self.update_location()


# 创建全局配置对象
settings = Settings()
